﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Architecture;
using Windows.UI.Core;
using System.Composition;
using System.Services;

namespace System.Architecture
{

    public class ViewModelBase : NotificationObject, IViewModel, IPersistedObject
    {
        public bool IsShared { get; private set; }
        public string CorrelationToken { get; private set; }
        public CoreDispatcher CurrentDispatcher { get; set; }
        public object Context { get; set; }


        [Import]
        public ISessionStateManager SessionState { get; set; }
        [Import]
        public ILoggerService Log { get; set; }

        protected ViewModelBase()
        {
        }

        [ImportingConstructor]
        public ViewModelBase(SessionStateManager SessionState)
        {
            var myType = this.GetType();
            var exportAttr = myType.GetCustomAttribute<ExportViewModelAttribute>(false);
            CorrelationToken = exportAttr == null ? string.Empty : exportAttr.CorrelationToken;
            IsShared = myType.GetCustomAttribute<SharedAttribute>(false) != null;

            OnInit();
        }

        public virtual void Save()
        {
            string key = string.Format("HDK.MVVM.{0}.{1}", this.CorrelationToken, "SomeProperty");
            this.SessionState.SetValue(key, "asdfgh");
        }

        public virtual void Load()
        {
            string key = string.Format("HDK.MVVM.{0}.{1}", this.CorrelationToken, "SomeProperty");
            var value = this.SessionState.GetValue(key);
        }


        protected virtual void OnInit()
        {
            Load();
        }

        public void OnViewLoaded()
        {
        }

        public void OnViewUnloaded()
        {
        }

        public void Dispose()
        {
        }

        public void Activated(object sender, EventArgs args)
        {
        }

        public void Deactivated(object sender, EventArgs args)
        {
        }
    }
}
